package net.huashitong.supervision.gongjian.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysUserDao;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.gongjian.dao.GjDeptProjectDao;
import net.huashitong.supervision.gongjian.dto.GjProjectDTO;
import net.huashitong.supervision.gongjian.entity.GjDeptProject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.UIDFolder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class GjDeptProjectService extends StringPKService<GjDeptProject> {
    @Autowired
    private SysTableService tableService;
    @Autowired
    private GjDeptProjectDao dao;
    @Autowired
    private SysDeptService deptService;

    @Autowired
    private SysUserDao userDao;


    @Override
    protected BaseDao<GjDeptProject, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(GjDeptProject obj) {
        String sysid = tableService.updateMaxSysId("t_gj_dept_project", null);
        obj.setSysId(sysid);
    }

    public List<GjProjectDTO> getProjectDeptList(String projectId, int deptType,int projectType)
    {
        List<GjProjectDTO> dtoList = new ArrayList<>();

        GjDeptProject filter = new GjDeptProject();
        filter.setProjectId(projectId);
        filter.setDeptType(deptType);
        filter.setProjectType(projectType);
        List<GjDeptProject> list = dao.getList(filter);
        for (GjDeptProject item : list) {
            SysDept dept = deptService.getById(item.getDeptId());
            GjProjectDTO dto = new GjProjectDTO();
            BeanUtils.copyProperties(item,dto);
            dto.setDeptName(dept.getDeptName());
            dtoList.add(dto);

        }

        return dtoList;
    }

    public String  getProjectDeptName(String projectId, int deptType,int projectType)
    {
        String deptName ="";

        GjDeptProject filter = new GjDeptProject();
        filter.setProjectId(projectId);
        filter.setDeptType(deptType);
        filter.setProjectType(projectType);
        List<GjDeptProject> list = dao.getList(filter);
        for (GjDeptProject item : list) {
            SysDept dept = deptService.getById(item.getDeptId());
            if(ObjectUtils.isNotBlank(dept))
            {
                deptName=dept.getDeptName()+"\n"+deptName;

            }
        }

        return deptName;
    }

    /**
     * 获取派发的单位信息
     * @param projectId
     * @param deptType
     * @param projectType
     * @return
     */
    public HashMap<String, Object> getProjectDept(HashMap<String,Object> map,String projectId, int deptType, int projectType)
    {
//        HashMap<String,Object> map = new HashMap<>();
        String deptName ="";

        GjDeptProject filter = new GjDeptProject();
        filter.setProjectId(projectId);
        filter.setDeptType(deptType);
        filter.setProjectType(projectType);
        List<GjDeptProject> list = dao.getList(filter);
        for (GjDeptProject item : list) {
            SysDept dept = deptService.getById(item.getDeptId());
            if(ObjectUtils.isNotBlank(dept))
            {
                map.put(dept.getSysId(),dept);

            }
        }

        return map;

    }


    /**
     * 获取单位主要主要领导
     */
    public List<SysUser> getDeptUser(List<SysUser> list ,String deptId,String personSignal){
        SysUserFilter filter = new SysUserFilter();
//        filter.setDeptId("20190722100000000110");
        filter.setDeptId(deptId);
        filter.setPersonSignal(personSignal);
//        filter.setPersonSignal("1");
        List<SysUser> userByDept = userDao.getUserByDept(filter);
//        userByDept.forEach(sysUser -> System.out.println(sysUser.getName()));
        list.addAll(userByDept);
        return list;
    }


}
